<include file="public@header_vue"/>
<style>
    .el-pagination {
        margin-top: 20px;
    }

    .avatar {
        width: 40px;
        height: auto;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
</style>
</head>
<body>
<div id="app">
    <el-tabs type="border-card">
        <el-tab-pane label="客户状态管理" v-loading="onDataLoading">
            <el-row>
                <el-col :span="4" style="text-align: left">
                    <el-button type="primary" size="mini" @click="update()">新增</el-button>
                </el-col>
            </el-row>
            <el-table :data="tableData" style="width: 100%">
                <el-table-column prop="id" label="ID" width="50"></el-table-column>
                <el-table-column prop="sort" label="排序" width="80">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.sort"
                                  @change="change_status('sort',scope.row.sort,scope.row.id)"></el-input>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="客户状态"></el-table-column>
                <el-table-column prop="type" label="所属状态标签">
                    <template slot-scope="scope">
                        {{ scope.row.type == 1 ? '已完成' : '未完成' }}
                    </template>
                </el-table-column>
                <el-table-column v-if="{$role_id}!=3" prop="company_name" label="公司名称"></el-table-column>
                <el-table-column prop="tag_list" label="标签">
                    <template slot-scope="scope">
                        <el-tag v-for=" tag in scope.row.tag_list">{{tag.name}}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="level" label="等级" width="80">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.level"
                                  @change="change_status('level',scope.row.level,scope.row.id)"></el-input>
                    </template>
                </el-table-column>
                <el-table-column prop="create_time" label="创建时间" min-width="120">
                    <template slot-scope="scope">
                        {{formatDate(scope.row.create_time)}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="180">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" @click="update(scope.row)">编辑
                        </el-button>
                        <el-button size="mini" v-if="scope.row.name !='成交' && scope.row.name !='意向客户' && scope.row.name !='丢单客户'" type="text"
                                   @click="delete_status(scope.row.id)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-tab-pane>
        <el-dialog :title="sourceFrom.id == '' ? '新增客户状态' : '修改客户状态'" :visible.sync="dialogVisible" width="60%"
                   v-loading.fullscreen="dialogLoading">
            <el-form :model="sourceFrom" class="demo-form-inline" :rules="rules" ref="sourceFrom"
                     label-width="200px">
                <el-form-item label="客户状态名" prop="name">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.name" :disabled="sourceFrom.name == '成交' || sourceFrom.name =='意向客户' || sourceFrom.name =='丢单客户' ? true : false"
                                  placeholder="客户状态名"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item v-if="{$role_id}!=3" label="选择公司" prop="company_id">
                    <el-col :span="18">
                        <el-select v-model="sourceFrom.company_id" placeholder="选择公司">
                            <el-option
                                    v-for="item in companyData"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-col>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-col :span="18">
                        <el-input type="textarea" v-model="sourceFrom.remark" placeholder="备注"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="所属状态标签" prop="type">
                    <el-col :span="18">
                        <el-radio-group v-model="sourceFrom.type">
                            <el-radio label="1">已完成</el-radio>
                            <el-radio label="0">未完成</el-radio>
                        </el-radio-group>
                    </el-col>
                </el-form-item>
                <el-form-item label="新增标签">
                    <el-col :span="18">
                        <el-button @click="add_tag">新增标签</el-button>
                    </el-col>
                </el-form-item>
                <el-form-item
                        v-for="(domain, index) in sourceFrom.tag_list"
                        :label="'标签' + index"
                        :key="domain.key"
                        prop="tag"
                >
                    <el-input v-model="domain.name" style="width:200px" placeholder="标签"></el-input>
                    <el-button @click.prevent="remove_tag(domain)">删除</el-button>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button v-if="sourceFrom.id === ''" type="primary" @click="submit('sourceFrom')">新增</el-button>
            <el-button v-else type="primary" @click="submit('sourceFrom')">修改</el-button>
        </span>
        </el-dialog>
    </el-tabs>
</div>
</body>
<script>
    let id = 1000;
    var app = new Vue({
        el: '#app',
        data: function () {

            var checkPhone = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('手机号不能为空'));
                } else {
                    const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
                    if (reg.test(value)) {
                        callback();
                    } else {
                        return callback(new Error('请输入正确的手机号'));
                    }
                }
            };
            let self = this;
            return {
                tableData: [],
                companyData: [],
                form: {
                    keyword: '',
                },
                sourceFrom: {
                    id: '',
                    name: '',
                    tag_list: [],
                },
                dialogVisible: false,
                dialogLoading: true,
                totalNum: 0,
                currentPage: 1,
                parent_info: [],
                show_name: {
                    label: 'name'
                },
                onDataLoading: false,
                headers: {'X-Requested-With': 'XMLHttpRequest'},
                rules: {
                    name: [{required: true, message: '请输入客户来源名称', trigger: 'blur'}],
                    company_id: [{required: true, message: '请选择公司', trigger: 'blur'}],
                },
            };

        },
        created() {
            this.getData()
            this.getCompanyData()
        },
        methods: {
            clearForm() {
                this.form = {
                    name: '',
                }
            },
            getCompanyData() {
                let _this = this;
                axios.get("{:url('admin/company/get_company_list')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                }).then(function (response) {
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.companyData = response.data.data;
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            update(data) {
                let _this = this;
                console.log(data)
                if (data) {
                    _this.sourceFrom = JSON.parse(JSON.stringify(data));
                } else {
                    _this.sourceFrom = {
                        id: '',
                        name: '',
                        tag_list: [],
                    }
                }
                _this.dialogVisible = true;
                _this.dialogLoading = false;
            },
            submit(form) {
                let _this = this;
                _this.$refs[form].validate((valid) => {
                    if (valid) {
                        axios.post("{:url('admin/customer_status/update')}", this.sourceFrom, {
                            headers: {
                                'X-Requested-With': 'XMLHttpRequest',
                            }
                        }).then(function (response) {
                            _this.dialogLoading = false;
                            if (response.data.code === 1) {
                                if (_this.sourceFrom.id === '') {
                                    _this.$message.success('新增完成');
                                } else {
                                    _this.$message.success('修改成功');
                                }
                                _this.getData();
                                _this.dialogVisible = false;
                            } else {
                                _this.$message.warning(response.data.msg);
                            }
                        }).catch(function (error) {
                            _this.dialogLoading = false;
                            _this.$message.error(error.toString())
                        })
                    } else {
                        _this.dialogLoading = false;
                        return false;
                    }
                });
            },
            remove(node, data) {
                let _this = this;
                const parent = node.parent;
                const children = parent.data.children || parent.data;
                const index = children.findIndex(d => d.id === data.id);
                console.log(children[index].children)
                if (children[index].children == undefined || children[index].children.length == 0) {
                    children.splice(index, 1);
                } else {
                    _this.$message.error('该角色下还有未删除的角色');
                }
            },

            //首页获取列表数据
            getData(page = 1) {
                let _this = this;
                let params = {page: page};
                this.onDataLoading = true;
                if (this.form.keyword !== '') {
                    params.keyword = this.form.keyword;
                }
                console.log(params);
                axios.get("{:url('admin/customer_status/index')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                    params: params
                }).then(function (response) {
                    console.log(response);
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.tableData = response.data.data;
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },

            change_status(field, value, id) {
                let _this = this;
                this.dialogLoading = true;
                params = {field: field, value: value, id: id};
                axios.post("{:url('admin/customer_status/change_status')}", params, {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    }
                }).then(function (response) {
                    _this.dialogLoading = false;
                    if (response.data.code === 1) {
                        _this.$message.success('修改完成');
                        _this.getData(_this.currentPage);
                    } else {
                        _this.$message.warning(response.data.msg);
                    }
                })
            },
            add_tag() {
                this.sourceFrom.tag_list.push({
                    value: '',
                    key: Date.now()
                });
            },
            remove_tag(item) {
                let index = this.sourceFrom.tag_list.indexOf(item)
                let _this = this;
                if (index !== -1) {
                    if (item['id']) {
                        _this.dialogLoading = true;
                        let params = {id: item['id']};
                        axios.post('/admin/customer_status/delete_tag', params, {
                            headers: {
                                'X-Requested-With': 'XMLHttpRequest',
                            }
                        }).then(function (response) {
                            _this.dialogLoading = false;
                            if (response.data.code === 1) {
                                _this.$message.success('删除完成');
                                _this.getData(_this.currentPage);
                            } else {
                                _this.$message.warning(response.data.msg);
                            }
                        }).catch(function (error) {
                            _this.dialogLoading = false;
                            _this.$message.error(error.toString())
                        })
                    }
                    this.sourceFrom.tag_list.splice(index, 1)
                }
            },
            delete_status(id) {
                let _this = this;
                _this.dialogLoading = true;
                if (id) {
                    params = {id: id};
                    axios.post('/admin/customer_status/delete', params, {
                        headers: {
                            'X-Requested-With': 'XMLHttpRequest',
                        }
                    }).then(function (response) {
                        _this.dialogLoading = false;
                        if (response.data.code === 1) {
                            _this.$message.success('删除完成');
                            _this.getData(_this.currentPage);
                        } else {
                            _this.$message.warning(response.data.msg);
                        }
                    }).catch(function (error) {
                        _this.dialogLoading = false;
                        _this.$message.error(error.toString())
                    })
                }
            },
            formatDate(timestamp) {
                let date = new Date(timestamp * 1000);
                return date.getFullYear() + '-' + this.prefix(date.getMonth() + 1) + '-' + this.prefix(date.getDate()) + ' ' + this.prefix(date.getHours()) + ':' + this.prefix(date.getMinutes()) + ':' + this.prefix(date.getSeconds());
            },
            prefix(num) {
                if (num < 10) {
                    return '0' + num;
                }
                return num;
            }
        }
    });
</script>
